package com.yunyi.user.controller;

import com.yunyi.response.CommonCode;
import com.yunyi.response.ResponseResult;
import com.yunyi.user.constant.Constant;
import com.yunyi.user.service.FileSystemService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @ClassName FileSystemController
 * @Description 文件处理系统
 * @Author leizhang
 * Date 2021/8/18 10:58 上午
 * @Version 1.0
 **/
@RestController
@RequestMapping("fileApi")
public class FileSystemController {
    private static Logger LOGGER = LoggerFactory.getLogger(FileSystemController.class);

    @Autowired
    private FileSystemService fileSystemService;


    /**
     * @param imgType 省份证上传 1 银行照片上传 2 省份证背面上传 3
     * @param file
     * @param openid
     * @param mimeType
     * @return com.yunyi.response.ResponseResult
     * @Description : 文件上传
     * @Author : leizhang
     * @Date 1:36 下午 2021/8/18
     **/
    @PostMapping("/fileUpload")
    public ResponseResult fileUpload(@RequestParam("imgType") int imgType, @RequestParam("file") MultipartFile file, @RequestParam("openid") String openid, @RequestParam("mimeType") String mimeType) {
        if (null == file || StringUtils.isBlank(openid)) {
            return new ResponseResult(CommonCode.FILE_UPLOAD_FAIL);
        }
        try {
            Map<String,Object> imgId = fileSystemService.uploadFile(imgType, file, openid, mimeType);

            String imgIdDesc= null;
            switch (imgType){
                case 1 :
                    imgIdDesc = "id_card_img_id";
                    break;
                case 2:
                    imgIdDesc = "bank_card_img_id";
                    break;
            }
            imgId.put(imgIdDesc,imgId.get(Constant.ID));
            ResponseResult  responseResult = new ResponseResult(CommonCode.SUCCESS);
            responseResult.setMessage(imgId);
            return responseResult;
        }catch (Exception e ) {
            LOGGER.error("文件上传失败信息:[{}]",e.getMessage(),e);
            return new ResponseResult(CommonCode.FAIL);
        }
    }
}
